<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class Recovery extends MX_Controller {

    function __construct() {
        parent::__construct();
    }

    private $_error_message;
    private $_success_message;

    
    public function recover() {

        if ($this->phpsession->get('is_logged_in'))
            redirect(base_url() . 'dashboard');

        $options = array();

        if ($this->is_postback())
            if (!$this->_do_recover())
                $options['error'] = $this->_error_message;
            else
                $options['error'] = $this->_success_message;

        $this->load->view('auth/recover', $options);
    }

    private function _do_recover() {

        $this->form_validation->set_rules('email', __('email'), 'trim|required|valid_email|xss_clean');
//        $this->form_validation->set_message('required', '%s ' . __('required_field'));
//        $this->form_validation->set_message('valid_email', '%s ' . __('valid_email_field'));
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if (!$this->form_validation->run()) {

            $this->_error_message = validation_errors();
            return FALSE;
        } else {

            $this->load->model('account/account_model');
            $users = $this->account_model->get_account(array('email' => $this->input->post('email')));

            if (count($users) == 1) {
                //TODO: Send email function must be called here


                $this->_success_message = '<div class="success">' . __('pw_recovery_success_message') . '</div>';
                return TRUE;
            } else {
                $this->_error_message = '<div class="error">' . __('pw_recovery_error_message') . '</div>';
                return FALSE;
            }
        }
    }

}